package com.smq.chatserver.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smq.chatserver.bean.Friendship;
import com.smq.chatserver.bean.User;
import com.smq.chatserver.mapper.FriendshipMapper;
import com.smq.chatserver.mapper.UserMapper;
import com.smq.chatserver.services.FriendshipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FriendshipServiceImpl extends ServiceImpl<FriendshipMapper, Friendship> implements FriendshipService {

    @Autowired
    FriendshipMapper friendshipMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> queryUser(String userId) {
        ArrayList<User> users = new ArrayList<>();
        List<Friendship> friendships = friendshipMapper.selectList(new QueryWrapper<Friendship>().eq("user_id", userId));
        friendships.forEach(friendship -> {
            if (friendship.getStatus().equals("accepted")) {
                users.add(userMapper.selectOne(new QueryWrapper<User>().eq("user_id", friendship.getFriendId())));
            }
        });
        return users;
    }
}
